package com.wiken.storeservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wiken.servicecommon.common.Code;
import com.wiken.servicecommon.common.TokenUtil;
import com.wiken.servicecommon.common.exception.DefaultException;
import com.wiken.storeservice.DTO.GeographicLocation;
import com.wiken.storeservice.VO.StoreActivityVO;
import com.wiken.storeservice.entity.Store;
import com.wiken.storeservice.entity.StoreActivity;
import com.wiken.storeservice.feign.UserServiceFeignClient;
import com.wiken.storeservice.mapper.StoreActivityMapper;
import com.wiken.storeservice.service.IStoreActivityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wiken
 * @since 2022-12-28
 */
@Service
public class StoreActivityServiceImpl extends ServiceImpl<StoreActivityMapper, StoreActivity> implements IStoreActivityService {

    @Autowired
    UserServiceFeignClient userServiceFeignClient;

    @Autowired
    StoreServiceImpl storeService;
    boolean checkAdministrator(String token){
        if(token == null) throw new DefaultException(Code.ERROR_AUTHORIZATION_NULL, "请重新登陆");

        Claims claims = TokenUtil.parseToken(token);

        if(claims == null) throw new DefaultException(Code.ERROR_AUTHORIZATION_NULL, "请重新登陆");

        String userId = claims.get("userId", String.class);

        if(userId == null) throw new DefaultException(Code.ERROR_AUTHORIZATION_NULL, "请重新登陆");

        Boolean success = userServiceFeignClient.getAdministratorById(userId);

        if(!success) throw new DefaultException(Code.ERROR_AUTHORIZATION, "认证失败，请重新登陆");

        return true;
    }

    boolean checkStoreKeepter(String storeId, String token){
        if(token == null) throw new DefaultException(Code.ERROR_AUTHORIZATION_NULL, "请重新登陆");

        Claims claims = TokenUtil.parseToken(token);

        if(claims == null) throw new DefaultException(Code.ERROR_AUTHORIZATION_NULL, "请重新登陆");

        String userId = claims.get("userId", String.class);

        if(userId == null) throw new DefaultException(Code.ERROR_AUTHORIZATION_NULL, "请重新登陆");

        Store store = storeService.getById(storeId);

        if(!store.getStorekeeperId().equals(userId)) throw new DefaultException(Code.ERROR_AUTHORIZATION, "认证失败，请重新登陆");

        return true;
    }

    @Override
    public boolean addStoreActivity(StoreActivity storeActivity, String token) {
        checkStoreKeepter(storeActivity.getStoreId(), token);
        storeActivity.setOverall(0);
        int success = baseMapper.insert(storeActivity);

        return success > 0;
    }

    @Override
    public boolean addStoreActivityByAdministrator(StoreActivity storeActivity, String token) {
        checkAdministrator(token);

        int success = baseMapper.insert(storeActivity);

        return success > 0;
    }

    @Override
    public Page<StoreActivity> getStoreActivityByStoreId(String storeId) {
        Page result = baseMapper.selectPage(new Page(), new QueryWrapper<StoreActivity>().eq("store_id", storeId));

        return result;
    }

    @Override
    public Page<StoreActivityVO> getStoreActivityByUserId(Page page, String token) {
        String userId = TokenUtil.getUserIdByToken(token);

        Page<StoreActivityVO> storeActivityList = baseMapper.getStoreActivityByUserId(page, userId);
        return storeActivityList;
    }

    @Override
    public Page<StoreActivity> getStoreActivityByAdministrator(String token) {
        checkAdministrator(token);
        Page page = new Page();

        Page result = baseMapper.selectPage(page, new QueryWrapper<StoreActivity>().eq("overall", 1));

        return result;
    }

    @Override
    public Page<StoreActivity> getStoreActivityList(Page page) {
        Page result = baseMapper.selectPage(page, null);
        return result;
    }

    @Override
    public boolean discountStoreNumberById(String id) {
        Integer success = baseMapper.discountStoreNumberById(id);

        return success > 0;
    }

    @Override
    public List<StoreActivityVO> getStoreActivityNearby(GeographicLocation geographicLocation) {
        List<String> ids = storeService.getStoreIdOfNearby(geographicLocation);
        List<StoreActivityVO> storeActivityVOList = baseMapper.getStoreActivityByIds(ids);
        return storeActivityVOList;
    }

}
